package cn.tedu.pdfamily.product.controller;

import cn.tedu.pdfamily.product.pojo.dto.CommentAddNewDTO;
import cn.tedu.pdfamily.product.pojo.vo.CommentListItemVO;
import cn.tedu.pdfamily.product.service.ICommentService;
import cn.tedu.pdfamily.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 处理评论相关请求的控制器
 *
 * @author Half_Summer
 * @version 0.0.1a
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/comments")
@Api(tags = "04. 评论管理模块")
public class CommentController {

    //注入CommentService接口
    @Autowired
    private ICommentService commentService;

    //输出日志: 对象已创建
    public CommentController() {
        log.debug("业务对象创建: ICommentService.");
    }

    @ApiOperation("添加评论")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    //[方法] 添加评论
    public JsonResult<Void> addNewComment(@Valid CommentAddNewDTO commentAddNewDTO) {
        log.debug("准备添加评论, 参数{}", commentAddNewDTO);
        commentService.addNewComment(commentAddNewDTO);
        log.debug("添加评论成功.");
        return JsonResult.ok();
    }

    @ApiOperation("根据id删除评论")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "评论id", required = true, dataType = "int")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> deleteById(@Range(min = 1, message = "删除评论失败, id不合法.") @PathVariable Long id) {
        log.debug("准备删除评论, id={}", id);
        commentService.deleteCommentById(id);
        return JsonResult.ok();
    }

    @ApiOperation("查询评论列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public JsonResult<List<CommentListItemVO>> commentList() {
        log.debug("开始处理【查询评论列表】的请求，无参数");
        List<CommentListItemVO> list = commentService.commentList();
        return JsonResult.ok(list);
    }

}
